package com.mz.ozy.mvp.presenter;


import android.app.Activity;
import android.content.Context;

import com.mz.ozy.activity.home.ScheduledDeliveryActivity;
import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.bean.AssetsBean;
import com.mz.ozy.bean.NetBean;
import com.mz.ozy.bean.ScheduledOrderBean;
import com.mz.ozy.bean.SortBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.model.AssetsModel;
import com.mz.ozy.mvp.model.ScheduledModel;
import com.mz.ozy.mvp.view.AssetsContract;
import com.mz.ozy.mvp.view.ScheduledContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.net.RetrofitUtils;
import com.mz.ozy.utils.JsonUtils;
import com.mz.ozy.utils.RxUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;

/**
 * Created by administrator on 2018/12/3.
 */
public class ScheduledPresenter extends BasePresenterImpl<ScheduledContract.View> implements ScheduledContract.Presenter {

    private ScheduledModel mModel;

    public ScheduledPresenter() {
        mModel = new ScheduledModel();
    }


    @Override
    public void geAssetsInfo(Activity activity, String city, String address, String pid, boolean isShow) {
        mModel.geAssetsInfo(activity, city, address, pid)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<SortBean>(mView, isShow) {
                    @Override
                    public void onSuccesss(SortBean bean) {
                        mView.geAssetsInfoSuccess(bean);
                    }
                });
    }


    @Override
    public void operateGoods(Activity activity, String id, String gid, String num, String type) {
        Map<String, String> maps = new HashMap<>();
        maps.put("id", id);
        maps.put("gId", gid);
        maps.put("num", num);
        RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10083, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<NetBean>(mView, false) {
                    @Override
                    public void onSuccesss(NetBean netBean) {
                        if ("1".equals(type)) {
                            mView.onEditGoodsCountSuccess("1");
                        } else if ("2".equals(type)) {
                            mView.onEditGoodsCountSuccess("2");
                        }
                    }
                });
    }

    @Override
    public void onBuy(Context context) {
        Map<String, String> maps = new HashMap<>();
        RetrofitUtils.getApiService(context).post(Constants.TransCode.TransCode10084, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<ScheduledOrderBean>(mView, true) {
                    @Override
                    public void onSuccesss(ScheduledOrderBean netBean) {
                        mView.onBuySuccess(netBean);
                    }
                });
    }
}
